apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../dialog"], function(V, $api, $, app, service, dialog) {
      app.ready();
        var vm = {
            el: '.container',
            data: {
                classifyList: [], //--分类之后的商品列表
                classfiyDown: [], //--分类下拉列表
                tabIndex: '',
                classfiyIndex: 0,
                classfiyId: '',
                on_sale: 1, //1为出售中，2为已下架
                page: 1,
                searchName: '',
                temGoodsName: '',
                goods_id: '',
            },
            mounted: function() {
                //--解决ios输入法挡住输入框的问题
                $(document).on("focus", "#aui-input", function() {
                    console.log($('body').scrollTop());
                    var top = $('body').scrollTop();
                    setTimeout(function() {
                        console.log(document.body.offsetHeight);
                        window.scrollTo(0, top + 100);
                    }, 50);
                });
            },
            methods: {
                //--搜索事件
                doSearch: function(et) {
                    this.page = 1;
                    this.searchName = $("#search-input").val();
                    var that = this;
                    //获取分类后的商品列表
                    this.productListGet(this.on_sale, this.page, this.classfiyId, function(res) {
                        that.classifyList = res.data;
                        if (res.data == '') {
                            $(".goods_mge_null").css("display", "block");
                            api.toast({
                                msg: '搜索不到任何商品...',
                                duration: 2000,
                                location: 'middle'
                            });
                        } else {
                            $(".goods_mge_null").css("display", "none");
                            api.toast({
                                msg: '搜索成功',
                                duration: 2000,
                                location: 'middle'
                            });
                        }

                    });
                },
                //--头菜单栏点击事件
                goodsMaeTab: function(indx) {
                    var that = this;

                    if (indx == 0) { //出售中
                        this.page = 1;
                        this.on_sale = 1;
                    } else if (indx == 1) { //已下架
                        this.page = 1;
                        this.on_sale = 2;
                    } else if (indx == 2) { //分类
                        this.page = 1;
                        this.on_sale = '';
                    }

                    this.productListGet(this.on_sale, this.page, this.classfiyId, function(res) {
                        if (res.data != '') {
                            that.classifyList = res.data;
                            $(".goods_mge_null").css("display", "none");
                        } else if (res.data == '') {
                            that.classifyList = [];
                            api.toast({
                                msg: '该分类下没有任何商品',
                                duration: 2000,
                                location: 'middle'
                            });
                            $(".goods_mge_null").css("display", "block");
                        }


                        that.tabIndex = indx;
                        window.scrollTo(0, 0); //--回到顶部

                    });

                },
                //--获取商品列表
                productListGet: function(on_sale, page, category_id, callback) { //获取商品列表
                    var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id
                    var on_sale = on_sale ? on_sale : this.on_sale;
                    var page = page ? page : this.page;
                    var category_id = category_id ? category_id : this.classfiyId;
                    var keyword = this.searchName;
                    var that = this;

                    app.ajax(service['productListGet'], {
                            data: {
                                values: {
                                    shop_code: shopId,
                                    unionid: unionid,
                                    on_sale: on_sale,
                                    page: page,
                                    category_id: category_id,
                                    keyword: keyword
                                }
                            }
                        },
                        function(res) {
                            if (res.code) {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            } else {
                                callback(res)
                                console.log("获取商品列表成功!");
                            }
                        });
                },
                //--前往商品详情
                open_detail: function(goods_id) {
                    app.openWin('shopAdd', {
                        shopId: api.pageParam.shopId,
                        goods_id: goods_id,
                        edit: 1
                    }, {
                        reload: true
                    })
                },
                //--添加商品
                goodsMangeAdd: function(et) {
                    app.openWin('shopAdd', {
                        shopId: api.pageParam.shopId
                    });
                },
                //--批量管理
                goodsManageBatch: function(tabIndex) {
                    app.openWin('batchManage', {
                        shopId: api.pageParam.shopId,
                        inds: tabIndex
                    });
                },
                //--新建分类
                newClassify: function() {
                    var that = this;
                    var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id

                    dialog.prompt({
                        title: "新建分类",
                        text: "输入分类名称",
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        // alert(JSON.stringify(ret));
                        if (ret.buttonIndex == 2) { //  确认
                            var name = $api.trim(ret.text);

                            if (name == '' || name == null) {
                                api.toast({
                                    msg: "请输入正确的分类名称...",
                                    duration: 2000,
                                    location: 'middle'
                                });
                                name.value = '';
                                return false;
                            } else if (name.length > 10) {
                                api.toast({
                                    msg: "名称字数超过上限...",
                                    duration: 2000,
                                    location: 'middle'
                                });
                                return false;
                            }
                            //--判断是否存在特殊字符
                            if (app.hasSpecialChar(name, '请正确输入分类名称...', '')) {
                                return
                            }

                            app.ajax(service['addCategory'], {
                                    data: {
                                        values: {
                                            shop_code: shopId,
                                            unionid: unionid,
                                            name: name
                                        }
                                    }
                                },
                                function(res) {
                                    //返回category_id
                                    if (res.code) {
                                        api.toast({
                                            msg: res.message,
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    } else {
                                        that.category_id = res.data;
                                        console.log(this.category_id);
                                        alert("新建分类成功！");
                                    }
                                });
                        }
                    })
                },
                //--分类管理
                classManageBatch: function() {
                    app.openWin('goodsSearch', {
                        shopId: api.pageParam.shopId
                    });
                },
                //分类下拉框显示
                classifyDown: function() {
                    // var el = et.currentTarget;	//js获取当前点击元素
                    // el.parentNode.dataset.show == "true"
                    this.getClassifyDown();
                    this.$refs.goodsMgeClassfiy.style.cssText += ' display: block';

                },
                //获取下拉分类列表
                getClassifyDown: function() {
                    var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id
                    var that = this;

                    app.ajax(service['getCategory'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    shop_code: shopId
                                }
                            }
                        },
                        function(res) {
                            // var shopCode = res.data.shop_code; // 商店id
                            if (res.code) {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            } else {
                                // console.log(JSON.stringify(res.data));
                                res.data.splice(0, 0, {
                                    'category_id': 0,
                                    'name': '默认排序'
                                });
                                console.log(JSON.stringify(res.data));
                                that.classfiyDown = res.data;
                            }
                        });
                },
                //--分类下拉点击
                classfiyDownLi: function(et) {
                    this.classfiyId = et.currentTarget.dataset.id;
                    this.classfiyIndex = et.currentTarget.dataset.index;

                    var ul = this.$refs.goodsMgeUl.children;
                    for (var i = 0; i < ul.length; i++) {
                        ul[i].classList.remove("active");
                    }
                    this.tabIndex = 2;
                    ul[2].classList.add("active");

                    var that = this;

                    this.page = 1;
                    this.on_sale = '';

                    //获取分类后的商品列表
                    this.productListGet(this.on_sale, this.page, this.classfiyId, function(res) {
                        that.classifyList = res.data;
                        if (res.data == '') {
                            api.toast({
                                msg: '该分类下没有任何商品',
                                duration: 2000,
                                location: 'middle'
                            });
                            $(".goods_mge_null").css("display", "block");
                        } else {
                            $(".goods_mge_null").css("display", "none");
                        }

                    });

                    //--获取分类id，
                    this.hide();
                },
                //--关闭分类
                goodsManageClassfiyClose: function(et) {
                    this.hide();
                },
                //--关闭分类下拉
                hide: function(et) {
                    this.$refs.goodsMgeClassfiy.style.cssText += 'display: none';
                    this.$refs.tabClassfiy.dataset.show = "true";
                },
                //--预览
                openBuyDetail: function(goodsId) {
                    app.openWin('buyGoods', {
                        goodsId: goodsId,
                        isSeller: 1
                    })
                },
                //--分享
                goodsManageShare: function(cover, goodsName, goods_id) {
                    this.$refs.goodsMangeShare.style.cssText += "top: 0;";
                    this.temGoodsCover = cover;
                    this.temGoodsName = goodsName;
                    this.goods_id = goods_id;
                    console.log(cover + ' , ' + goodsName + ' , ' + goods_id)
                },
                share: function(type) {
                    var that = this;
                    var unionid = $api.getStorage('shop_unionid');
                    var goods_id = this.goods_id;
                    console.log(this.temGoodsName + ' and ' + this.temGoodsCover)

                    if (type == 'friend') {
                        var scene = 'session';
                    } else if (type == 'circle') {
                        var scene = 'timeline';
                    }
                    //--获取H5页面链接
                    app.ajax(service['Share'], {
                            data: {
                                values: {
                                    type: 2,
                                    unionid: unionid,
                                    share_id: goods_id
                                }
                            }
                        },
                        function(res) {
                            // var shopCode = res.data.shop_code; // 商店id
                            if (res.code) {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            } else {
                                console.log(JSON.stringify(res.data));
                                var contentUrl = res.data;
                                wx = api.require('wx');
                                wx.shareWebpage({
                                    scene: scene,
                                    // title: $api.getStorage('user').data.nickname+'给你发送了名片，期待与您成为好友！',
                                    title: $api.getStorage('user').data.nickname + '给你发送了商品' + that.temGoodsName,
                                    description: '600万高端人脉都在使用的有张名片，高端人脉，引流，交友的聚集地！',
                                    // thumb: 'widget://image/logoo.png',
                                    thumb: '',
                                    contentUrl: contentUrl
                                }, function(ret, err) {
                                    if (ret.status) {
                                        // $('.tc_box_win').css("background", "rgba(0, 0, 0, 0.2)")
                                        // $('.getmoney_box').css("display", "block")
                                        api.toast({
                                            msg: '分享成功:',
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                        that.temGoodsName = '';
                                        that.temGoodsCover = '';
                                        that.goods_id = '';
                                    } else {
                                        api.toast({
                                            msg: '分享失败',
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    }
                                });
                            }
                        });


                },
                //--关闭分享
                goodsMangeShareClose: function(et) {
                    this.$refs.goodsMangeShare.style.cssText += "top: 100%;";
                },
                //--删除已下架商品
                productDel: function(goods_id) {
                    var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id
                    var that = this;

                    dialog.alert({
                        title: "商品回收提示",
                        msg: '请确认是否删除该商品',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            app.ajax(service['productDel'], {
                                    data: {
                                        values: {
                                            shop_code: shopId,
                                            unionid: unionid,
                                            goods_id: goods_id
                                        }
                                    }
                                },
                                function(res) {
                                    //返回category_id
                                    if (res.code) {
                                        api.toast({
                                            msg: res.message,
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    } else {
                                        api.toast({
                                            msg: '商品已删除',
                                            duration: 2000,
                                            location: 'middle'
                                        });

                                        that.productListGet(that.on_sale, that.page, that.classfiyId, function(res) {
                                            that.classifyList = res.data;
                                            if (res.data == '') {
                                                api.toast({
                                                    msg: '该分类下没有任何商品',
                                                    duration: 2000,
                                                    location: 'middle'
                                                });
                                                $(".goods_mge_null").css("display", "block");
                                            } else {
                                                $(".goods_mge_null").css("display", "none");
                                            }

                                        });
                                    }
                                });
                        }
                    });
                },
                //--上架已下架商品
                productOnSale: function(goods_id) {
                    var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id
                    var that = this;

                    dialog.alert({
                        title: "商品上架提示",
                        msg: '请确认是否上架该商品',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            app.ajax(service['productOnSale'], {
                                    data: {
                                        values: {
                                            shop_code: shopId,
                                            unionid: unionid,
                                            goods_id: goods_id,
                                            on_sale: 1
                                        }
                                    }
                                },
                                function(res) {
                                    //返回category_id
                                    if (res.code) {
                                        api.toast({
                                            msg: res.message,
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    } else {
                                        api.toast({
                                            msg: '商品已上架',
                                            duration: 2000,
                                            location: 'middle'
                                        });

                                        that.productListGet(that.on_sale, that.page, that.classfiyId, function(res) {
                                            that.classifyList = res.data;
                                            if (res.data == '') {
                                                api.toast({
                                                    msg: '该分类下没有任何商品',
                                                    duration: 2000,
                                                    location: 'middle'
                                                });
                                                $(".goods_mge_null").css("display", "block");
                                            } else {
                                                $(".goods_mge_null").css("display", "none");
                                            }
                                        });
                                    }
                                });
                        }
                    });
                },
                // 触底
                scrolltobottom: function() {
                    var that = this;
                    this.page = parseInt(this.page + 1);

                    this.productListGet(this.on_sale, this.page, this.classfiyId, function(res) {
                        if (res.data != '') {
                            that.classifyList = that.classifyList.concat(res.data);
                        } else {
                            if (that.page != 1) {
                                that.page = parseInt(that.page - 1);
                                api.toast({
                                    msg: '没有更多数据...',
                                    duration: 2000,
                                    location: 'middle'
                                });
                            }
                        }
                    });
                }
            },
            created: function() {
                var that = this;

                //获取出售中商品列表
                this.productListGet(1, 1, '', function(res) {
                    if (res.data != '') {
                        that.classifyList = that.classifyList.concat(res.data);
                    } else {
                        $(".goods_mge_null").css("display", "block");
                    }
                    console.log(JSON.stringify(that.classifyList));
                    console.log(that.classifyList.constructor);
                    this.tabIndex = 0;
                });

                api.addEventListener({
                    name: 'keyboardshow'
                }, function(ret, err) {
                    $('.header').css({
                        'position': 'static'
                    });
                    $(".container.top-container").css("cssText", "padding-top: 0rem !important;");
                    $('.aui-searchbar').css({
                        'position': 'static'
                    });
                    $('.goods_mge_tab').css({
                        'position': 'static'
                    });
                    $('.goods_mge_null').css({
                        'position': 'static'
                    });
                    $('.goods_mge_list').css({
                        'padding-top': '0'
                    });
                });

                api.addEventListener({
                    name: 'keyboardhide'
                }, function(ret, err) {
                    $('.header').css({
                        'top': 0,
                        'position': 'fixed'
                    });
                    $(".container.top-container").css("cssText", "padding-top: 3rem !important;");
                    $('.aui-searchbar').css({
                        'top': '3rem',
                        'position': 'fixed'
                    });
                    $('.goods_mge_tab').css({
                        'top': '5.2rem',
                        'position': 'fixed'
                    });
                    $('.goods_mge_null').css({
                        'position': 'relative'
                    });
                    $('.goods_mge_list').css({
                        'padding-top': '4.4rem'
                    });
                });

                api.addEventListener({
                    name: 'scrolltobottom',
                    extra: {
                        threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
                    }
                }, function(ret, err) {
                    that.scrolltobottom()
                });

                api.addEventListener({
                    name: 'backToGoodsManage',
                }, function(ret, err) {
                    console.log('response')
                        // vm.data.on_sale = ret.value.on_sale;
                        // vm.data.page = ret.value.page;
                        // vm.data.classfiyId = ret.value.classfiyId;
                    that.productListGet(that.on_sale, that.page, that.classfiyId, function(res) {
                        that.classifyList = res.data;
                        if (res.data == '') {
                            api.toast({
                                msg: '该分类下没有任何商品',
                                duration: 2000,
                                location: 'middle'
                            });
                            $(".goods_mge_null").css("display", "block");
                        } else {
                            $(".goods_mge_null").css("display", "none");
                        }

                    });

                });

                //下拉刷新
                app.refreshHeaderInfo(function() {
                    that.page = 1;

                    that.productListGet(that.on_sale, that.page, that.classfiyId, function(res) {
                        that.classifyList = res.data;
                        if (res.data == '') {
                            api.toast({
                                msg: '该分类下没有任何商品',
                                duration: 2000,
                                location: 'middle'
                            });
                            $(".goods_mge_null").css("display", "block");
                        } else {
                            $(".goods_mge_null").css("display", "none");
                        }

                    });
                });

            }
        };
        // var shopVue = new Vue(vm);
        var even = function() {




            // dialog = new auiDialog({});
        };
        var newVue = new Vue(vm);
        even();

    })
}
